/* ====================================================================
 * (#)Header: PropertyComboBoxRenderer.java
 * (#)Revision: 
 * (#)Date Created: Mar 2, 2007 12:07:22 AM
 * (#)Created By: Karol Wozniak
 * ====================================================================
 *
 * Copyright (c) 2007 karolak .  All rights reserved.
 *
 * ====================================================================
 * 
 * The software application represented here is the sole property of 
 * karolak. Its unauthorized utilization is expressly prohibited.
 * 
 * ====================================================================
 */
package com.karolak.client;

import java.awt.Component;

import javax.swing.JList;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

import com.karolak.bean.BeanUtils;

/**
 * <code>PropertyComboBoxRenderer</code> is used to ....
 * 
 * @author <a href="mailto:kwozniak@hotmail.com">Karol Wozniak</a>
 */
public class PropertyComboBoxRenderer<T> extends BasicComboBoxRenderer {

	private static final long serialVersionUID = 1L;

	private Class<T> typeClass;
	private String propertyName;

	public PropertyComboBoxRenderer(Class<T> typeClass, String propertyName) {
		this.typeClass = typeClass;
		this.propertyName = propertyName;
	}

	@Override
	public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

		// setHorizontalAlignment(JLabel.CENTER);
		if (getTypeClass() != null && getPropertyName() != null && value != null) {
			if (getTypeClass().isAssignableFrom(value.getClass())) {
				Object propertyValue = BeanUtils.getPropertyValue(value, getPropertyName());
				if (propertyValue != null && propertyValue instanceof String) {
					value = ((String) propertyValue).trim();
				} else {
					value = propertyValue;
				}
			}
		}
		return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
	}

	protected String getPropertyName() {
		return propertyName;
	}

	protected Class<T> getTypeClass() {
		return typeClass;
	}
}
